package com.project.service.impl;

import com.project.bean.CutPageBean;
import com.project.bean.RoomBean;
import com.project.dao.IFacilityDao;
import com.project.dao.IRoomDao;
import com.project.dao.IStudentDao;
import com.project.service.IRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoomServiceImpl implements IRoomService {

    @Autowired
    private IRoomDao roomDao;
    @Autowired
    private IStudentDao studentDao;
    @Autowired
    private IFacilityDao facilityDao;

    @Override
    public CutPageBean<RoomBean> findByItem(int pageNO, String address, String state, String info, String type) {
        return roomDao.findByItem(pageNO, address, state, info, type);
    }

    @Override
    public void add(RoomBean room) {
        roomDao.add(room);

    }

    @Override
    public boolean del(Integer roomId) {
        int studentNum = studentDao.countByRoom(roomId);
        if (studentNum == 0 ) {
            facilityDao.delByRoom(roomId);
            roomDao.del(roomId);
            return true;
        } else {
            return false;
        }
    }

    @Override
    public RoomBean findByRoomId(Integer id) {

        RoomBean roomBean = roomDao.findByRoomId(id);
        roomBean.setStudent(studentDao.findByRoomId(id));
        return roomBean;
    }

    @Override
    public List<RoomBean> findByGender(String gender) {
        return roomDao.findByGender(gender);
    }
}
